<template>
	<!--表格单选-->
	<el-row class="category">
  		<el-col :span="24">
  			<!--查询过滤条件-->
  			<el-form :inline="true" :model="conditionForm" ref="conditionForm" label-width="82px">
  				<el-form-item label="名称">				
					<!-- <el-input v-model="conditionForm.categoryName"></el-input> -->
					<m-input-auto-comp :inputData="conditionForm" :queryInterUrl="urlPaths.queryList" queryParamName="categoryName"/>
				</el-form-item>
				<el-button type="primary" size="small" icon="search" @click="queryTableData">查询</el-button>
			</el-form>
  		</el-col>
  		<el-col :span="24">
  			<!--表格数据展示-->
  			<div>
	  			<el-button type="primary" size="small" icon="plus" v-on:click="addFn">新增</el-button>
	  			<el-button type="primary" size="small" icon="edit" v-on:click="editFn">修改</el-button>
	  			<el-button type="primary" size="small" icon="delete" v-on:click="deleteFn">删除</el-button>
  			</div>
  			<div class="data-list">
				<m-table-complex :tableDatas="tableDatalist" :queryTableDataUrl="urlPaths.queryList" :queryTableDataParms="conditionForm" :tableColumn="tableColumn"
					ref="tableChild" @select-rowidx-handler="handleCurrentChange">
				</m-table-complex>
				<!-- <el-table :data="tableDatalist.slice((pagination.currentPage-1)*pagination.pageSize,pagination.currentPage*pagination.pageSize)" 
					border style="width: 100%" highlight-current-row @current-change="handleCurrentChange">
					<el-table-column width="50">
						<template slot-scope="scope">
							<label class="radio">
								<input type="radio" value="2" name="radio" :data-index="scope.row.index">
								<span class="dot"></span>
							</label>
						</template>
					</el-table-column>
					<el-table-column v-for="column in tableColumn" :prop="column.columnName" :label="column.columnLabel" 
						v-if="column.isColumnShow" :show-overflow-tooltip="true" sortable>
						<template slot-scope="scope" >
							<div v-if="column.isPicColumn">
								<img v-if="isPicPath(scope.row, column.columnName)" :src="scope.row[column.columnName]" :alt="scope.row[column.columnName]" style="width: 50px;height: 50px">
								<i v-else class="el-icon-picture-outline" style="width: 50px;height: 50px"></i>
							</div>
							<div v-else>
								{{ scope.row[column.columnName] }}
							</div>
						</template>
					</el-table-column>
				</el-table>
				<el-pagination
			      @size-change="pageSizeChange"
			      @current-change="handleCurrentChangePage"
			      :current-page="pagination.currentPage"
			      :page-sizes="[10,20, 50, 100, 200]"
			      :page-size="pagination.pageSize"
			      layout="->,prev, pager, next, jumper, sizes,total"
			      :total="pagination.totalSize">
			    </el-pagination> -->
			</div>
  		</el-col>
  		<el-col :span="24">
  			<!--新增和修改 dialog-->
			<el-dialog :title="dialogTitle" :visible.sync="dialogVisible" customClass="customStyle">
				<el-form :model="configform" ref="configform" :rules="rules" label-width="100px">
					<!--<el-form-item v-for="(columnValue, columnKey) in configform" :label="transAttrName(columnKey)" v-if="isEditEnabled(columnKey)">
						<el-col :span="12">
							<el-select v-if="isShowSelectList(columnKey)" v-model="configform[columnKey]" filterable placeholder="请选择">
								<el-option v-for="item in getSelectOption(columnKey)" :key="item.id" :label="item.name" :value="item.id"></el-option>
							</el-select>
							<el-input v-else style="width:100%" v-model="configform[columnKey]"
								:clearable="true" :readonly="isShowUploadPic(columnKey)"></el-input>
						</el-col>
						<el-col :span="12" v-if="isShowUploadPic(columnKey)">
							<m-upload-img :imgsAttrName="uploadDatas" :itemAttr="columnKey" :isMultImgs="false" 
								ref="uploadChild" @response-handler="afterUploadHandler">
							</m-upload-img>
						</el-col>
					</el-form-item>-->
					<m-dialog-form-item v-for="column in tableColumn" :configform="configform" :columnInfo="column"
						:uploadImgAttr="uploadDatas">
					</m-dialog-form-item>
				</el-form>
				<div slot="footer" class="dialog-footer">
					<el-button @click="closeDialog">取 消</el-button>
					<el-button type="primary" @click="saveSubmit">确 定</el-button>
				</div>
			</el-dialog>
			<!-- <m-dialog-newandmod :dialogTitle="dialogTitle" :dialogVisible="dialogVisible" 
				:configform="configform" :tableColumn="tableColumn" :urlPaths="urlPaths"
				:imgsAttrName="uploadDatas" :itemAttr="columnKey" :isMultImgs="false"
				:getSelectOption="getSelectOption" :submitCallback="submitCallback">
			</m-dialog-newandmod> -->
		</el-col>
	</el-row>
</template>
<script>
	import {
		Message
	} from 'element-ui';
	export default {
		components:{},
		data() {
			return {
				tableDatalist:[],
				conditionForm: { //查询过滤的条件
					categoryName:''
				},
				tableColumn:[ 
					{
						columnName:'id', //主键字段，修改和删除的依赖列
						columnLabel:'ID',
						isColumnShow:false, //是否在table表格里显示
						isModifyDialogEnabled:false, //是否在新增或修改对话框中显示该属性字段
					},
					{
						columnName:'name',
						columnLabel:'类名',
						isColumnShow:true, //是否在table表格里显示
						isModifyDialogEnabled:true, //是否在新增或修改对话框中显示该属性字段
					},
					{
						columnName:'keywords',
						columnLabel:'搜索关键字',
						isColumnShow:false, //是否在table表格里显示
						isModifyDialogEnabled:true,
					},
					{
						columnName:'level',
						columnLabel:'分类级别',
						isColumnShow:true, //是否在table表格里显示
						isModifyDialogEnabled:false,
//						isInputSelectList:true,
//						selectListData:[],
//						// selectListData:[{'id':'L1', 'name':'L1级别'},{'id':'L2', 'name':'L2级别'}],
//						dataQueryUrl:'category/findAllLevel'
					},
					{
						columnName:'parentId',
						columnLabel:'父类（L1级别不用填，默认是0）',
						isColumnShow:true, //是否在table表格里显示
						isModifyDialogEnabled:true,
						isInputSelectList:true,
						defaultValue:'0',
						selectListData:[],
						dataQueryUrl:'category/findAllParent'
					},
					{
						columnName:'type',
						columnLabel:'type',
						isColumnShow:false, //是否在table表格里显示
						isModifyDialogEnabled:false,
						defaultValue:'0'
					},
					{
						columnName:'sortOrder',
						columnLabel:'搜索排序',
						isColumnShow:true, //是否在table表格里显示
						isModifyDialogEnabled:true,
						defaultValue:'1',
					},
					{
						columnName:'showIndex',
						columnLabel:'排序顺序',
						isColumnShow:true, //是否在table表格里显示
						isModifyDialogEnabled:true,
						defaultValue:'1',
					},
					{
						columnName:'isShow',
						columnLabel:'是否显示',
						isColumnShow:true, //是否在table表格里显示
						isModifyDialogEnabled:true,
						defaultValue:'1',
					},
					{
						columnName:'bannerUrl',
						columnLabel:'横幅图片地址1090*360',
						isColumnShow:true, //是否在table表格里显示
						isModifyDialogEnabled:true,
						isPicColumn:true
					},
					{
						columnName:'iconUrl',
						columnLabel:'图标图片地址90*90',
						isColumnShow:true, //是否在table表格里显示
						isModifyDialogEnabled:true,
						isPicColumn:true
					},
					{
						columnName:'imgUrl',
						columnLabel:'图片地址720*160',
						isColumnShow:true, //是否在table表格里显示
						isModifyDialogEnabled:true,
						isPicColumn:true
					},
					{
						columnName:'wapBannerUrl',
						columnLabel:'图片地址750*246',
						isColumnShow:true, //是否在table表格里显示
						isModifyDialogEnabled:true,
						isPicColumn:true
					},
					{
						columnName:'frontName',
						columnLabel:'说明',
						isColumnShow:true, //是否在table表格里显示
						isModifyDialogEnabled:true,
					},
					{
						columnName:'frontDesc',
						columnLabel:'描述',
						isColumnShow:true, //是否在table表格里显示
						isModifyDialogEnabled:true,
					}
				],
				urlPaths:{
					queryList:'category/listAll',
					delByKey:'category/delete',
					insert:'category/add',
					updateByKey:'category/update',
				},
				configform: {},
				isNeedReQuery:false,
				index: '',
				dialogVisible: false,
				dialogTitle:'新增',
				rules: {
				},
				currentRow: null,
//				tableData.slice((pagination.currentPage-1)*pagination.pageSize,pagination.currentPage*pagination.pageSize)
				pagination:{
					totalSize:0,
					pageSize: 20,
					currentPage:1,
				},
				uploadImgs: {},
				uploadDatas:{
					clazz:'category',
					columnName:''
				}
			}
		}, 
		computed:{
			/**
			 * @author：01367604
			 * @function: 转换表单中显示的属性名称，将英文列名转换成中文描述
			 * @pramas: 
			 */
			transAttrName(value){
				return function (value){
//					#console.log(value);
					var colElem = this.tableColumn.find((element) => (element.columnName == value))
					if (colElem.columnLabel != undefined && colElem.columnLabel.length>0){
						return colElem.columnLabel;
					} else {
						return colElem.columnName;
					}
				}
			},
			/**
			 * @author：yc
			 * @function: 转换表中显示的图片属性，将其转换成图片展示
。			 * @pramas: 
			 */
			isPicPath(value, columnName){
				return function (value, columnName){
					return value[columnName]!=undefined && value[columnName].length>0;
				}
			},
			/**
			 * @author：01367604
			 * @function: 当鼠标移动到图片地址时，显示图片缩略图
			 * @pramas: 
			 */
			showSlotPic() {
				
			},
			/**
			 * @author：yc
			 * @function: 新增和修改model框是否显示（编辑）某属性
			 * @pramas: 
			 */
			isEditEnabled(value){
				return function (value){
					var colElem = this.tableColumn.find((element) => (element.columnName == value))
					return colElem.isModifyDialogEnabled != undefined && colElem.isModifyDialogEnabled;
				}
			},
			/**
			 * @author：01367604
			 * @function: 新增和修改model框中显示下拉框
			 * @pramas: 
			 */
			isShowSelectList(value){
				return function (value){
					var colElem = this.tableColumn.find((element) => (element.columnName == value))
					return colElem.isInputSelectList != undefined && colElem.isInputSelectList;
				}
			},
			/**
			 * @author：yc
			 * @function: 新增和修改model框中是否上传图片组件
			 * @pramas: 
			 */
			isShowUploadPic(value){
				return function (value){
					return(value === 'imgUrl' || value==='wapBannerUrl' 
					    || value==='iconUrl' || value==='bannerUrl');
				}
			}
		},
		beforeCreate(){
    	},
    	mounted() {
    		this.initData();
		},
		methods: {
			initData(){
    			// this.queryTableData();
			},
// 			queryTableData(){
// 				/*查询接口*/
// 				this.$httpExt().get(this.urlPaths.queryList, {
// //					pageSize:this.pagination.pageSize,
// //					currentPage:this.pagination.currentPage,
// 					categoryName:this.conditionForm.categoryName
// 				}).then((response) => {
// 					var rows= response.rows;
// 					console.log(rows);
// 					for(let [index, item] of rows.entries()){
// 						item.index = index
// 					}
// 					this.tableDatalist = rows;
// 					this.pagination.totalSize = response.totalSize;
// //					this.pagination.totalSize = this.tableDatalist.length;
// 				},(response) => {
// 					Message({
// 						message: '服务器请求失败，请重试！',
// 						type: 'warning'
// 					});
// 				});
// 			},
			queryTableData(){
				console.log(this.conditionForm);
				this.$refs.tableChild.initData();
			},
			/*选择一条数据*/
			handleCurrentChange(val) {
				if(!val) {
					return
				}
				this.currentRow = val;
				this.index = val.index+''
				console.log('this.currentRow ',this.currentRow);
				console.log('this.index ',this.index);
			},
			/*新增*/
			addFn(){
				this.dialogVisible = true;
				this.dialogTitle='新增';
				/*if(this.$refs['configform']!=undefined) {
					this.$refs['configform'].resetFields();
				}*/
				this.setFormData();
			},
			/*编辑*/
			editFn(){
				if(this.index) {
					this.dialogVisible = true;
					this.dialogTitle='编辑';
					this.setFormData();
				}else{
					Message({
						message: '请选择一条进行操作！',
						type: 'warning'
					});
				}
			},
			setFormData(){
				var form ={};
				for(let col of this.tableColumn){
//					if (col.isModifyDialogEnabled){
//						if (this.dialogTitle=='新增'){
//							form[col.columnName] = '';
//						} else {
//							form[col.columnName] = this.currentRow[col.columnName];
//						}
//					}
					if (this.dialogTitle=='新增'){
						if (col.defaultValue != undefined && col.defaultValue!=''){
							form[col.columnName] = col.defaultValue;
						} else {
							form[col.columnName] = '';
						}
					} else {
						form[col.columnName] = this.currentRow[col.columnName];
					}
				}
				this.configform = form;
			},
			/*删除*/
			deleteFn(){
				if(this.index) {
					this.$confirm('你确定要删除吗？', '删除提示', {
			          	confirmButtonText: '确定',
			          	cancelButtonText: '取消',
			          	type: 'warning'
			        }).then(() => {
			        	//调删除接口
			        	console.log(this.currentRow.id);
			        	this.$httpExt().get(this.urlPaths.delByKey, {
							id:this.currentRow.id
						}).then((response) => {
//							Message({
//								message: response.msg,
//								type: 'success'
//							});
							this.queryTableData();
						},(response) => {
							Message({
								message: '服务器请求失败，请重试！',
								type: 'warning'
							});
						});
			        });
				}else{
					Message({
						message: '请选择一条进行操作！',
						type: 'warning'
					});
				}
			},
			/*提交*/
			saveSubmit(){				
//				this.$refs.configform.validate((valid) => {
//					if(valid) {
//						
//						}
//					}
//				});
				var that = this;
				if(this.dialogTitle == '新增') {
//							that.configform.id = '';
					var url = this.urlPaths.insert;
				} else {
					var url = this.urlPaths.updateByKey;
				}
				console.log(url);
				console.log(that.configform);
				this.$httpExt().post(url,that.configform).then((response) => {
					if(response.succ == 'ok') {
						that.queryTableData();
						that.dialogVisible = false;
					}
				}, (data) => {
					Message({
						message: data.msg ? data.msg : '服务器请求失败，请重试！',
						type: 'warning'
					});
				});
//				this.tableDatalist.push(that.configform);
			},
			closeDialog(){
				this.dialogVisible = false;
			},
//			cleanUploadFile(){
//				// console.log(this.$refs.uploadChild);
//				if (this.$refs.uploadChild==undefined){
//					return;
//				} else if (this.$refs.uploadChild instanceof Object){
//					this.$refs.uploadChild.cleanUploadFile();
//				} else if (this.$refs.uploadChild instanceof Array){
//					for (var uploadChildCom of this.$refs.uploadChild){
//						uploadChildCom.cleanUploadFile();
//					}
//				}
//			},
			handleReset() {
				this.newlyFormVisible = false
			},
			afterUploadHandler(data){
				// console.log(data);
				this.configform[data.columnName] = data.res;
			},
			getSelectOption(value){
				//console.log(value);
				if (value === 'parentId'){
					var l1Category =[];
					for (var item of this.tableDatalist){
						if (item.level==='L1'){
							l1Category.push(item);
						}
					}
	//						var l1Category= this.tableDatalist.some((element, index, array) => (element.level === 'L1'));
					return l1Category;
				} else if (value === 'level'){
					var levelOption = [];
					var l1 = {'id':'L1', 'name':'L1级别'};
					var l2 = {'id':'L2', 'name':'L2级别'};
					levelOption.push(l1);
					levelOption.push(l2);
					return levelOption;
				}
			}
		}
	}
</script>
<style>
.fl{
	float: left;
}

.category a{
	color: #20a0ff;
	text-decoration: none;
}
</style>